﻿using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;

namespace MyWordAddIn
{
    class DesEncryptionHelper
    {

        private static readonly string strKey = HexStr2ByteArr();

        public static string Encrypt(string data, string secret)
        {
            try
            {
                return ByteArr2HexStr(Encrypt(secret, Encoding.UTF8.GetBytes(data)));
            }
            catch (Exception e)
            {
                return string.Empty;
            }
        }

        public static string Encrypt(string data)
        {
            try
            {
                return ByteArr2HexStr(Encrypt(strKey, Encoding.UTF8.GetBytes(data)));
            }
            catch (Exception e)
            {
                return string.Empty;
            }
        }

        public static string Decrypt(string strIn)
        {
            byte[] decryptByte;
            string decryptString = string.Empty;

            try
            {
                decryptByte = HexStr2ByteArr(strIn);
                byte[] bytes = Decrypt(strKey, decryptByte);
                decryptString = Encoding.UTF8.GetString(bytes, 0, bytes.Length);
            }
            catch (Exception e)
            {
                return string.Empty;
            }

            return decryptString;
        }

        public static string Decrypt(string strIn, string secret, out bool status)
        {
            byte[] decryptByte;
            string decryptString = string.Empty;

            try
            {
                decryptByte = HexStr2ByteArr(strIn);
                byte[] bytes = Decrypt(secret, decryptByte);
                decryptString = Encoding.UTF8.GetString(bytes, 0, bytes.Length);
            }
            catch (Exception e)
            {
                status = false;
                return string.Empty;
            }
            status = true;
            return decryptString;
        }

        private static byte[] Encrypt(string key, byte[] data)
        {

            try
            {
                //定义DES加密服务提供类
                DESCryptoServiceProvider des = new DESCryptoServiceProvider();
                des.Mode = CipherMode.ECB;
                des.Padding = PaddingMode.PKCS7;
                KeySizes[] keys = des.LegalBlockSizes;

                //加密密匙转化为byte数组
                byte[] pwdBytes = Encoding.UTF8.GetBytes(key);
                byte[] keyBytes = new byte[8];
                int len = pwdBytes.Length;
                if (len > keyBytes.Length)
                {
                    len = keyBytes.Length;
                }
                System.Array.Copy(pwdBytes, keyBytes, len);
                // des.IV = byteKey;
                //创建其支持存储区为内存的流
                MemoryStream ms = new MemoryStream();
                //定义将数据流链接到加密转换的流
                CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(keyBytes, null), CryptoStreamMode.Write);
                cs.Write(data, 0, data.Length);
                cs.FlushFinalBlock();
                cs.Close();
                ms.Close();
                return ms.ToArray();
            }
            catch (Exception e)
            {
                return null;
            }
        }

        private static byte[] Decrypt(string key, byte[] data)
        {
            try
            {
                //定义DES加密解密服务提供类
                DESCryptoServiceProvider des = new DESCryptoServiceProvider();
                des.Mode = CipherMode.ECB;
                des.Padding = PaddingMode.PKCS7;

                //加密密匙转化为byte数组
                byte[] pwdBytes = Encoding.UTF8.GetBytes(key);
                byte[] keyBytes = new byte[8];
                int len = pwdBytes.Length;
                if (len > keyBytes.Length)
                {
                    len = keyBytes.Length;
                }
                System.Array.Copy(pwdBytes, keyBytes, len);

                //创建其支持存储区为内存的流
                MemoryStream ms = new MemoryStream();
                //定义将数据流链接到解密转换的流
                CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(keyBytes, null), CryptoStreamMode.Write);
                cs.Write(data, 0, data.Length);
                cs.FlushFinalBlock();
                cs.Close();
                ms.Close();

                return ms.ToArray();
            }
            catch (Exception e)
            {
                return null;
            }
        }

        //byte 数组 转换为 字符串
        private static string ByteArr2HexStr(byte[] arrB)
        {
            int iLen = arrB.Length;
            StringBuilder sb = new StringBuilder(iLen * 2);

            for (int i = 0; i < iLen; i++)
            {
                int intTmp = arrB[i];
                while (intTmp < 0)
                {
                    intTmp = intTmp + 256;
                }
                if (intTmp < 16)
                {
                    sb.Append("0");
                }
                sb.Append(Convert.ToString(intTmp, 16));
            }
            return sb.ToString();
        }

        public static byte[] HexStr2ByteArr(string strIn)
        {
            byte[] arrB = UTF8Encoding.UTF8.GetBytes(strIn);
            int iLen = arrB.Length;

            byte[] arrOut = new byte[iLen / 2];
            for (int i = 0; i < iLen; i = i + 2)
            {
                string strTmp = UTF8Encoding.UTF8.GetString(arrB, i, 2);
                arrOut[i / 2] = Convert.ToByte(int.Parse(strTmp, System.Globalization.NumberStyles.HexNumber));
            }
            return arrOut;
        }

        private static string HexStr2ByteArr()
        {
            return "BDC109F59D423A4787A4F37D9CADF0900121506273ECBA8716158DEC49082CF2DF29FB29734843F0C5D106D273AF00BE9".Substring(10, 33);
        }
    }
}
